import {env} from "../../env.js";
import axios from "axios";
import {ElLoading, ElMessage, ElNotification} from "element-plus";
import {reactive} from "vue";


const api = axios.create({
    baseURL:env.base_url,
    timeout: 1000,
    responseType:'json',
    withCredentials:'',
    headers:{
        'Content-Type':'application/json;charset=utf-8'
    }
})
// 记录当前页⾯请求总次数
let loadingRequestCount = 0;
// 初始化loading
let loadingInstance;
const showLoading = () => {
    loadingInstance = ElLoading.service({
            lock: true,
            text: 'loading',
            spinner: '',
            background: 'rgba(0, 0, 0, 0.5)'
    });
};
const closeLoading=()=>{
    loadingInstance.close()
}
// 前置拦截器（发起请求之前的拦截）
axios.interceptors.request.use(
    (loadingInstance) => {
        showLoading();
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

// 响应拦截器
const responseInterceptor = api.interceptors.response.use(
  function (response) {
      // closeLoading();
      const data = response.data;
      if (data.code===200){
          console.log("成功");
          ElMessage({
              message: data.message,
              type: 'success',
          })
      }else{
          console.log("失败");
          ElNotification({
              title: 'Error',
              message: data.message,
              type: 'error',
          })

      }
      return data
  },function (error) {
        return Promise.reject(error)
    }
)
// // 移除请求拦截器
// api.interceptors.request.eject(requestInterceptor);
//
// // 移除响应拦截器
// api.interceptors.response.eject(responseInterceptor);

export default api;